# -*- coding: utf-8 -*-


import getopt ,sys
# 定义短格式命令行参数
# u username  p password s score c city
# python pygetopt.py    -a  -u lacnk -p 123456  -c sc
#
#opts,args = getopt.getopt(sys.argv[1:], 'au:p:s:f:c:')

#for opt in opts:
#    print("end=====================================end")
#    print(opt)
#
#print(opts)
#print("*"*20)
#print(args)
#


# 定义长命令格式
# python pygetopt.py --user zs --password 12345678
# python pygetopt.py --user=zs --password=12345678
#mopts,margs = getopt.getopt(sys.argv[1:], '',['user=','password='])
#
#for mopt in mopts:
#    print("end=====================================end")
#    print(mopt)
#
#print(mopts)
#print("*"*20)
#print(margs)
#

# 定义长、短命令格式一起指定
# python pygetopt.py --user zs --password 12345678
# python pygetopt.py -u zs -p 12345678
#mopts,margs = getopt.getopt(sys.argv[1:], '-u:-p:',['user=','password='])
#
#for mopt in mopts:
#    print("end=====================================end")
#    print(mopt)
#
#print(mopts)
#print("*"*20)
#print(margs)
#

# 定义长、短命令格式一起指定，并读取参数数据
# python pygetopt.py --user zs --password 12345678
# python pygetopt.py -u zs -p 12345678
#opts,margs = getopt.getopt(sys.argv[1:], '-u:-p:',['user=','password='])
#
## 获取命令行参数值
#for opt,arg in opts:
#    print("end=====================================end")
#    if opt in ("-u","--user"):
#        user = arg
#    elif  opt in ("-p","--password"):
#        passw = arg
#
#    print(opt)
#
#
#print(opts)
#print("*"*20)
#print(margs)
#
#print(user)
#print(passw)
#


# 定义长、短命令格式一起指定，并读取参数数据,并进行异常捕获
#python pygetopt.py -a bc  d --user zs --pa sds
# python pygetopt.py --user zs --password 12345678
# python pygetopt.py -u zs -p 12345678
# python pygetopt.py -u zs -p 12345678

try:
    opts,margs = getopt.getopt(sys.argv[1:], '-u:-p:',['user=','password='])

except getopt.GetoptError as err:
    print("get args is error",err)
    sys.exit(1)

# 获取命令行参数值
for opt,arg in opts:
    print("end=====================================end")
    if opt in ("-u","--user"):
        user = arg
    elif  opt in ("-p","--password"):
        passw = arg

    print(opt)


print(opts)
print("*"*20)
print(margs)

print(user)
print(passw)


